package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.CreditCardLogic;
import com.hotel.BL.CreditCardTypeLogic;
import com.hotel.BL.ReservationLogic;

import data.CreditCard;
import data.CreditCradType;
import data.Group;
import data.Reservation;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class CreditCardService extends HttpServlet {
	// http://localhost:8080/CreditCardService?func=addCreditCard&CVC=123&cardType=Credit
	// Card&NameOnCard=test&cardNumber=123456&ExpiryDate=07/56
	// http://localhost:8080/CreditCardService?func=editCreditCard&creditCardId=865&CVC=123&cardType=Credit
	// Card&NameOnCard=test&cardNumber=123456&ExpiryDate=07/56
	// http://localhost:8080/CreditCardService?func=delCreditCard&creditCardId=865
	// http://localhost:8080/CreditCardService?func=changeCreditCard&creditCardId=865
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("addCreditCard")) {
				AddCreditCard(req, resp);
			} else if (doClass.equals("editCreditCard")) {
				EditCreditCard(req, resp);
			} else if (doClass.equals("delCreditCard")) {
				DeleteCreditCard(req, resp);
			} else if (doClass.equals("changeCreditCard")) {
				ChangeStatus(req, resp);
			} else if (doClass.equals("loadCreditCard")) {
				LoadCreditCard(req, resp);
			}else if (doClass.equals("addFullCreditCard")) {
				AddFullCreditCard(req, resp);
			}else if (doClass.equals("suggestCardNumber")) {
				SuggestCardNumber(req, resp);
			}else if (doClass.equals("loadCreditCardType")) {
				LoadCreditCardType(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void AddCreditCard(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session = req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		long creditCardId = Long.parseLong(req.getParameter("creditCardId"));
		long groupId = 0;
		if(req.getParameter("groupId") != null){
			 groupId = Long.parseLong(req.getParameter("groupId"));
		}
		long resId = 0;
		if(req.getParameter("resId") != null ){
			resId = Long.parseLong(req.getParameter("resId"));
		}
		String CVC = req.getParameter("CVC");
		String cardType = req.getParameter("cardType");
		String NameOnCard = req.getParameter("NameOnCard");
		String cardNumber = req.getParameter("cardNumber");
		String ExpiryDate = req.getParameter("ExpiryDate");
		CreditCard c = null;
		if (creditCardId == 0) {
			c = new CreditCardLogic(req,resp).AddCreditCard(hotelId, CVC, cardType,
					NameOnCard, cardNumber, ExpiryDate);		
		} else {
			c = new CreditCardLogic(req,resp).EditCreditCard(creditCardId, CVC,
					cardType, NameOnCard, cardNumber, ExpiryDate);
		}
		if(groupId != 0){
			new ReservationLogic(req,resp).EditCreditCard(groupId,
					String.valueOf(c.getId()));
		}else{
			new ReservationLogic(req,resp).EditCreditCard2Res(resId, String.valueOf(c.getId()));
		}
		
		Gson gson = new Gson();
		boolean z = false;
		if (c != null) {
			z = true;
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(z));
	}

	private void AddFullCreditCard(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String CVC = req.getParameter("cvc");
		String CardType = req.getParameter("cardtype");
		String NameOnCard = req.getParameter("nameoncard");
		String CardNumber = req.getParameter("cardnumber");
		String ExpiryDate = req.getParameter("expiredate");
		String sharer = req.getParameter("sharer");
		String address = req.getParameter("address");
		String state = req.getParameter("state");
		String country = req.getParameter("country");
		String city = req.getParameter("city");
		String zipcode = req.getParameter("zipcode");
		String guestId = req.getParameter("guestId");
		CreditCard c = new CreditCard();
			c = new CreditCardLogic(req,resp).AddFullCreditCard( hotelId,  CVC,  CardType,NameOnCard,  CardNumber,  ExpiryDate, sharer,
					 address, state, country, city, zipcode,guestId);
				String CCN = new CreditCardLogic(req,resp).enCodeCreditCardNumber(c.getCardNumber());
				HashMap<String,Object> hash = com.Util.toHashmap(c); 
				hash.remove("key");
				hash.remove("Address");
				hash.remove("City");
				hash.remove("Country");
				hash.remove("Zipcode");
				hash.remove("State");
				hash.remove("CVC");
				hash.remove("CardNumber");
				hash.remove("status");
				hash.remove("hotelId");
				hash.put("CardNumber",CCN);
				hash.put("id",c.getId());
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hash));
	}
	
	private void EditCreditCard(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException {
		long creditCardId = Long.parseLong(req.getParameter("creditCardId"));
		String CVC = req.getParameter("CVC");
		String cardType = req.getParameter("cardType");
		String NameOnCard = req.getParameter("NameOnCard");
		String cardNumber = req.getParameter("cardNumber");
		String ExpiryDate = req.getParameter("ExpiryDate");

		CreditCard c = new CreditCardLogic(req,resp).EditCreditCard(creditCardId, CVC,
				cardType, NameOnCard, cardNumber, ExpiryDate);
		String CCN = new CreditCardLogic(req,resp).enCodeCreditCardNumber(c.getCardNumber());
		HashMap<String,Object> hash = com.Util.toHashmap(c); 
		hash.remove("key");
		hash.remove("Address");
		hash.remove("City");
		hash.remove("Country");
		hash.remove("Zipcode");
		hash.remove("State");
		hash.remove("CVC");
		hash.remove("CardNumber");
		hash.remove("status");
		hash.remove("hotelId");
		hash.put("CardNumber",CCN);
		hash.put("id",c.getId());
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hash));

	}

	private void DeleteCreditCard(HttpServletRequest req,
			HttpServletResponse resp) throws IOException, JSONException,
			ServletException, EntityNotFoundException {
		long creditCardId = Long.parseLong(req.getParameter("creditCardId"));

		boolean obj = new CreditCardLogic(req,resp).DeleteCreditCard(creditCardId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException {
		long creditCardId = Long.parseLong(req.getParameter("creditCardId"));

		Object obj = new CreditCardLogic(req,resp).ChangeStatus(creditCardId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void LoadCreditCard(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException {
		CreditCard c = new CreditCard();
		
		if(req.getParameter("groupId") != null){
			long groupId = Long.parseLong(req.getParameter("groupId"));
			
			Group g = new ReservationLogic(req,resp).GetGroup(groupId);
			if (g.getCreditString().equals("0")) {
				c = null;
			} else {
				c = new CreditCardLogic(req,resp).GetCreditCard(Long.parseLong(g
						.getCreditString()));
			}
			Gson gson = new Gson();
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(c));
		}else{
			long resId = Long.parseLong(req.getParameter("resId"));
			
			Reservation g = new ReservationLogic(req,resp).GetReservation(resId);
			if (g.getCreditString().equals("0")) {
				c = null;
			} else {
				c = new CreditCardLogic(req,resp).GetCreditCard(Long.parseLong(g
						.getCreditString()));
			}
			Gson gson = new Gson();
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(c));
		}

	}
	
	private void SuggestCardNumber(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException {
		String text = req.getParameter("text");
		Object obj = null;
		String[] array = null;
		if(text.equals("1")){
			array = new String[] { "10", "11", "12", "13", "14", "15"};
		}else if(text.equals("2")){
			array = new String[] { "20", "21", "22", "23", "24", "25"};
		}else if(text.equals("3")){
			array = new String[] { "30", "31", "32", "33", "34", "35"};
		}else if(text.equals("4")){
			array = new String[] { "40", "41", "42", "43", "44", "45"};
		}
		
		List<String> list = Arrays.asList(array);
		
		obj = list;
		
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void LoadCreditCardType(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException {
		List<CreditCradType> c = new CreditCardTypeLogic().GetAllCreditCradType();
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
}